#!/bin/bash

echo "========================================="
echo "开始安装和配置数据库模块"
echo "========================================="

# 1. 清理旧的编译文件
echo ""
echo "步骤 1/4: 清理旧的编译文件..."
rm -rf node_modules/better-sqlite3/build

# 2. 重新安装 better-sqlite3
echo ""
echo "步骤 2/4: 重新安装 better-sqlite3..."
npm uninstall better-sqlite3
npm install better-sqlite3@9.2.2

# 3. 使用 electron-rebuild 重新编译
echo ""
echo "步骤 3/4: 使用 electron-rebuild 重新编译..."
npx electron-rebuild -f -w better-sqlite3

# 4. 验证安装
echo ""
echo "步骤 4/4: 验证安装..."
if [ -d "node_modules/better-sqlite3/build/Release" ]; then
    echo "✅ better-sqlite3 编译成功！"
    ls -lh node_modules/better-sqlite3/build/Release/
else
    echo "❌ better-sqlite3 编译失败！"
    exit 1
fi

echo ""
echo "========================================="
echo "✅ 数据库模块安装完成！"
echo "========================================="
echo ""
echo "现在可以运行: npm run dev"
